package com.xuan.core.service.pg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.core.dto.pg.PgBaZiDto;
import com.xuan.core.mapper.pg.PgBaZiMapper;
import com.xuan.core.pojo.pg.PgBaZi;
import com.xuan.core.service.pg.PgBaZiService;
import com.xuan.core.service.sys.SysCountService;
import com.xuan.core.vo.pg.PgBaZiVo;
import com.xuan.utils.xuan.bazi.BaZi;
import com.xuan.utils.xuan.bazi.BaZiSetting;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 八字排盘Impl
 */
@Service
public class PgBaZiServiceImpl extends ServiceImpl<PgBaZiMapper, PgBaZi> implements PgBaZiService {

    @Autowired
    private SysCountService sysCountService; // 系统统计Service

//********************************************************************************************************************************

    /**
     * 开始排盘
     *
     * @param dto 八字排盘数据接收类
     * @return 排盘结果
     */
    @Override
    public PgBaZiVo starts(PgBaZiDto dto) {

        // 1、拷贝八字排盘设置
        BaZiSetting setting = new BaZiSetting();
        BeanUtils.copyProperties(dto, setting);

        // 2、八字排盘
        BaZi baZi = new BaZi(setting);

        // 3、增加1次总使用数
        sysCountService.saveUsageNumber();

        // 4、封装八字排盘数据并返回
        return pgBaZiVoPackage(dto, baZi);

    }

//================================================================================================================================

    /**
     * 八字排盘数据封装
     *
     * @param dto  八字排盘数据接收类
     * @param baZi 八字排盘
     * @return 排盘结果
     */
    private PgBaZiVo pgBaZiVoPackage(PgBaZiDto dto, BaZi baZi) {

        PgBaZiVo vo = new PgBaZiVo();
        BeanUtils.copyProperties(dto, vo); // 设置的数据
        BeanUtils.copyProperties(baZi, vo); // 计算的数据
        vo.setSex(dto.getSex() == 0 ? "女" : "男"); // 性别
        vo.setZao(dto.getSex() == 0 ? "坤造" : "乾造"); // 乾造或坤造
        return vo;

    }


}
